package com.baidu.bmfmap.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.baidu.bmfmap.R;


public class RoundLayoutMap extends RelativeLayout {
    private float roundLayoutRadius = 14.0f;
    private Path roundPath;
    private RectF rectF;

    private int startColor = -1;
    private int endColor = -1;

    public RoundLayoutMap(Context context) {
        this(context, null);
    }

    public RoundLayoutMap(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundLayoutMap);
        roundLayoutRadius = typedArray.getDimensionPixelSize(R.styleable.RoundLayoutMap_crl_radius, (int) roundLayoutRadius) + 0.0f;

        startColor = typedArray.getColor(R.styleable.RoundLayoutMap_crlStartColor, startColor);
        endColor = typedArray.getColor(R.styleable.RoundLayoutMap_crlEndColor, endColor);

        typedArray.recycle();

        init();
    }

    private void init() {
        setWillNotDraw(false);//如果你继承的是ViewGroup,注意此行,否则draw方法是不会回调的;
        roundPath = new Path();
        rectF = new RectF();
    }

    private void setRoundPath() {
        //添加一个圆角矩形到path中, 如果要实现任意形状的View, 只需要手动添加path就行
        roundPath.addRoundRect(rectF, roundLayoutRadius, roundLayoutRadius, Path.Direction.CW);
        roundPath.close();
    }


    public void setRoundLayoutRadius(float roundLayoutRadius) {
        this.roundLayoutRadius = roundLayoutRadius;
        setRoundPath();
        postInvalidate();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        rectF.set(0f, 0f, getMeasuredWidth(), getMeasuredHeight());
        setRoundPath();
    }

    @Override
    public void draw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);

        if (roundLayoutRadius > 0f) {
            canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
            canvas.clipPath(roundPath);
        }

        if (!(-1 == startColor && -1 == endColor)) {
            if (-1 == startColor) startColor = endColor;
            if (-1 == endColor) endColor = startColor;

            //获取View的宽高
            float width = getWidth();
            float height = getHeight();
            LinearGradient backGradient = new LinearGradient(0.0f, 0.0f, width, height, new int[]{startColor,/* color1,*/ endColor}, null, Shader.TileMode.CLAMP);
            paint.setShader(backGradient);
            canvas.drawRect(0f, 0f, width, height, paint);
        }

        super.draw(canvas);
    }
}